package weaver.interfaces.qz.job;

import cn.hutool.core.date.DateUtil;
import com.engine.qz.util.QzUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.ThreadPoolUtil;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2024/3/12
 * @Description 更新餐补信息记录数据定时任务
 * @Author AdminZm
 */
public class UpdateCQDataJob extends BaseCronJob {

    BaseBean baseBean = new BaseBean();

    private String ryParam;

    private String startDate;

    private String endDate;

    @Override
    public void execute() {
        baseBean.writeLog("UpdateCQDataJob start, param is:" + ryParam + "、" + startDate + "、" + endDate);
        try {
            String today = DateUtil.format(new Date(), "yyyy-MM-dd");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = dateFormat.parse(today);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, -2); // 减去两天
            Date previousDay = calendar.getTime();
            String date1 = dateFormat.format(previousDay);

            calendar.setTime(date);
            calendar.add(Calendar.DATE, -1); // 减去一天
            Date previousDay1 = calendar.getTime();
            String date2 = dateFormat.format(previousDay1);

            calendar.setTime(date);
            calendar.add(Calendar.DATE, -3); // 减去三天
            Date previousDay3 = calendar.getTime();
            String date3 = dateFormat.format(previousDay3);

            QzUtil qzUtil = new QzUtil();
            List<String> allDates = qzUtil.getAllDates(date3, date2);
            baseBean.writeLog("UpdateCQDataJob allDatas:" + allDates);
            RecordSet rs = new RecordSet();
            List<String> userIds = new ArrayList<>();
            Map<String, String> statusMap = new HashMap<>();
            if (StringUtils.isEmpty(ryParam) || StringUtils.equals(ryParam, "null")) {
                baseBean.writeLog("1111.");
                rs.executeQuery("select id, status from hrmresource ");
                while (rs.next()) {
                    String idT = rs.getString("id");
                    if (StringUtils.isEmpty(idT)) {
                        continue;
                    }
                    String statusT = rs.getString("status");
                    if (StringUtils.isEmpty(statusT)) {
                        statusT = "0";
                    }
                    userIds.add(idT);
                    statusMap.put(idT, statusT);
                }
            } else {
                baseBean.writeLog("2222.");
                rs.executeQuery("select id, status from hrmresource where workcode = ?", ryParam);
                while (rs.next()) {
                    String idT = rs.getString("id");
                    if (StringUtils.isEmpty(idT)) {
                        continue;
                    }
                    String statusT = rs.getString("status");
                    if (StringUtils.isEmpty(statusT)) {
                        statusT = "0";
                    }
                    userIds.add(idT);
                    statusMap.put(idT, statusT);
                }
            }
            baseBean.writeLog("userIds:" + userIds);
            if ((CollectionUtils.isEmpty(userIds) && userIds.isEmpty()) || (CollectionUtils.isEmpty(allDates) && allDates.isEmpty())) {
                baseBean.writeLog("no user or date.");
                return;
            }

            //获取休息班班次
            List<String> xxShiftList = new ArrayList<>();
            rs.executeQuery("select bc from uf_kqxxbcb ");
            while (rs.next()) {
                String bc = Util.null2String(rs.getString("bc"));
                if (StringUtils.isNotBlank(bc)) {
                    xxShiftList.add(bc);
                }
            }

            // 获取离职日期
            Map<String, String> lzMap = new HashMap<>();
            rs.execute("select resourceid, changedate from HRMSTATUSHISTORY  where type_n = 5");
            while (rs.next()) {
                String resourceid = rs.getString("resourceid");
                String changedate = rs.getString("changedate");
                if (StringUtils.isEmpty(resourceid) || StringUtils.isEmpty(changedate)) {
                    continue;
                }
                lzMap.put(resourceid, changedate);
            }

            // 获取存在请假或者出差或者公出的人员及日期
            Set<String> existSet = qzUtil.getHrmRqSet(startDate, endDate);
            baseBean.writeLog("HandleCQDataA start.");
            for (String userId : userIds) {
                String statusT = "0";
                if ((!CollectionUtils.isEmpty(statusMap)) && statusMap.containsKey(userId)) {
                    statusT = statusMap.get(userId);
                }
                if (StringUtils.isEmpty(statusT)) {
                    statusT = "0";
                }
                for (String kqDate : allDates) {
                    ThreadPoolUtil.getThreadPool("HandleCQDataA", "5").execute(() -> {
                        qzUtil.handleCQData(userId, kqDate, xxShiftList, existSet, lzMap, statusMap);
                    });
                }
            }
            baseBean.writeLog("UpdateCQDataJob end.");
        } catch (Exception e) {
            baseBean.writeLog("UpdateCQDataJob error:" + e.getMessage());
        }
    }
}
